﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;

namespace simpleDns
{
    public partial class systemConfig : Form
    {
        public systemConfig()
        {
            InitializeComponent();
            tbDataPath.Text = settings.dataPath;
            tbDns1.Text = settings.dns1;
            tbDns2.Text = settings.dns2;
            tbServerIp.Text = settings.serverIp;
            tbTcpnum.Text = settings.tcpNum.ToString();
            tbUdpnum.Text = settings.udpNum.ToString();
            tbTrash.Text = settings.cacheTrush.ToString();
            tbTTL.Text = settings.ttl_default.ToString();
            cbHide.Checked = settings.autoHide;
            cbRun.Checked = settings.autoRun;
            cbStart.Checked = settings.autoStart;
        }

        private void tbSave_Click(object sender, EventArgs e)
        {
            if (!(check(tbServerIp) && check(tbDns1) && check(tbUdpnum) && check(tbDataPath)))
            {
                MessageBox.Show("必填项目");
                return;
            }
            if (!settings.isIpAddress(tbServerIp.Text.Trim()))
            {
                MessageBox.Show("监听IP填写错误");
                return;
            }
            if (!settings.isIpAddress(tbDns1.Text.Trim()))
            {
                MessageBox.Show("监听IP填写错误");
                return;
            }
            if (tbDns2.Text.Trim().Length>0 && !settings.isIpAddress(tbDns2.Text.Trim()))
            {
                MessageBox.Show("接锅DNS2填写错误");
                return;
            }
            if (!settings.isIpAddress(tbServerIp.Text.Trim()))
            {
                MessageBox.Show("监听IP填写错误");
                return;
            }

            Properties.Settings.Default.dataPath = tbDataPath.Text.Trim();
            Properties.Settings.Default.dns1= tbDns1.Text.Trim();
            Properties.Settings.Default.dns2= tbDns2.Text.Trim();
            Properties.Settings.Default.serverIp = tbServerIp.Text.Trim();
            Properties.Settings.Default.tcpNum= Convert.ToInt32(tbTcpnum.Text.Trim());
            Properties.Settings.Default.udpNum = Convert.ToInt32(tbUdpnum.Text.Trim());
            Properties.Settings.Default.cacheTrush=Convert.ToInt32(tbTrash.Text.Trim());
            Properties.Settings.Default.ttl_default=Convert.ToInt32(tbTTL.Text.Trim());
            Properties.Settings.Default.autoHide= cbHide.Checked;
            Properties.Settings.Default.autoRun= cbRun.Checked;
            Properties.Settings.Default.autoStart= cbStart.Checked;
            Properties.Settings.Default.Save();
            autoStart(cbStart.Checked);
            this.DialogResult = DialogResult.OK;
        }

        private void selectPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fb = new FolderBrowserDialog();
            fb.SelectedPath = settings.dataPath;
            if (fb.ShowDialog() == DialogResult.OK)
            {
                tbDataPath.Text = fb.SelectedPath;
            }
        }
        private bool check(Control ct)
        {
            if (string.IsNullOrEmpty(ct.Text.Trim()))
            {
                ct.BackColor = Color.Red;
                return false;
            }
            return true;
                
        }

        private void tbCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void autoStart(bool auto)
        {
            string sc = Environment.CurrentDirectory + @"\simpledns.lnk";
            string ds = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "simpledns.lnk");
            if (auto)
            {
                if (File.Exists(sc))
                    File.Delete(sc);
                if (File.Exists(ds))
                    File.Delete(ds);
                creatShotcat();
                File.Move(sc, ds);
            }
            else
            {
                if (File.Exists(ds))
                    File.Delete(ds);
            }
            
        }
        private void creatShotcat()
        {
            Shortcut link = new Shortcut();
            link.Description = "simpledns startup link,delete if you don't need";
            link.Path = this.GetType().Assembly.Location;
            link.Save("simpledns.lnk");
        }
    }
}
